/*
 * CEnergyRegen.java
 * 
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 * 
 * This file is part of Expression program is undefined on line 8, column 40 in Templates/Licenses/license-gplv3.txt..
 * 
 * Expression program is undefined on line 10, column 19 in Templates/Licenses/license-gplv3.txt. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Expression program is undefined on line 15, column 19 in Templates/Licenses/license-gplv3.txt. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Expression program is undefined on line 21, column 30 in Templates/Licenses/license-gplv3.txt..  If not, see <http://www.gnu.org/licenses/>.
 */

package dpssystem;

import dpsstats.CEnergyStats;
import java.util.ArrayList;

/**
 *
 * @author Mani
 */
public class CEnergyRegen {

    private CSim m_sim;
    public int m_currentEnergy = 100;
    public int m_maxEnergy = 100;
    public int m_regenRate = 1;
    private double m_focusedAttacksChance;
    private boolean m_focuseAttacksEnabled = false;
    private double m_relentlessStrikesChance;
    private boolean m_relentlessStrikesEnabled = false;
    private boolean m_tier8bonus = false;
    private boolean m_combatPotency = false;
    private int m_combatPotencyExtraEnergy = 0;
    private double m_vitality = 0.1;

    CEnergyStats m_autoRegenStats;
    CEnergyStats m_cappedEnergyStats;
    CEnergyStats m_focusedAttacksStats;
    CEnergyStats m_relentlessStrikesStats;
    CEnergyStats m_combatPotencyStats;
    CEnergyStats m_tier8_2PieceBonusStats;


    CEnergyRegen(CSim p_sim)
    {
        m_sim = p_sim;
        if (p_sim.m_dpsInterface.m_talents.getVigor() != 0)
        {
            m_maxEnergy += 10;
            if (p_sim.m_dpsInterface.m_glyphs.m_vigor)
            {
                m_maxEnergy += 10;
            }
        }

        if (m_sim.m_dpsInterface.m_talents.getFocusedAttacks() != 0)
        {
            m_focuseAttacksEnabled = true;
            m_focusedAttacksChance = m_sim.m_dpsInterface.m_talents.getFocusedAttacksDouble() * (100.0 / 3.0);
        }

        if (m_sim.m_dpsInterface.m_talents.getRelentlessStrikes() != 0)
        {
            m_relentlessStrikesEnabled = true;
            m_relentlessStrikesChance = m_sim.m_dpsInterface.m_talents.getRelentlessStrikesDouble() * 4;
        }

        if (m_sim.m_dpsInterface.m_bonusSet.isTier8_2pc())
        {
            m_tier8bonus = true;
        }

        if (m_sim.m_dpsInterface.m_talents.getCombatPotency() != 0)
        {
            m_combatPotencyExtraEnergy = m_sim.m_dpsInterface.m_talents.getCombatPotency() * 3;
        }

        if (m_sim.m_dpsInterface.m_talents.getVitality() == 1)
        {
            m_vitality *= 0.92;
        } else if (m_sim.m_dpsInterface.m_talents.getVitality() == 2)
        {
            m_vitality *= 0.86;
        } else if (m_sim.m_dpsInterface.m_talents.getVitality() == 3)
        {
            m_vitality *= 0.75;
        }
        
        // Careful if adding a new stat for this, it must match the sequence
        // where they are added in CAllStats class!
        m_autoRegenStats = m_sim.m_stats.m_energyStats.get(0);
        m_cappedEnergyStats = m_sim.m_stats.m_energyStats.get(1);
        m_focusedAttacksStats = m_sim.m_stats.m_energyStats.get(2);
        m_relentlessStrikesStats = m_sim.m_stats.m_energyStats.get(3);
        m_combatPotencyStats = m_sim.m_stats.m_energyStats.get(4);
        m_tier8_2PieceBonusStats = m_sim.m_stats.m_energyStats.get(5);
    }

    public void setEnergyRegenRate(int p_rate)
    {
        // Gets called as part of adrenaline rush
        m_regenRate = p_rate;
    }

    public void autoEnergyRegen()
    {
        addEnergy(m_regenRate);
        m_autoRegenStats.addEnergy(m_regenRate);
    }

    public void focusedAttacks()
    {
        if (m_focuseAttacksEnabled)
        {
            if (m_sim.m_rng.nextDouble() <= m_focusedAttacksChance)
            {
                this.addEnergy(2);
                m_focusedAttacksStats.addEnergy(2);
            }
        }
    }

    public void combatPotency()
    {
        if (m_combatPotency)
        {
            if (m_sim.m_rng.nextDouble() <= 0.20)
            {
                this.addEnergy(m_combatPotencyExtraEnergy);
                m_combatPotencyStats.addEnergy(m_combatPotencyExtraEnergy);
            }
        }
    }
    // Run after a finisher. It is important that the finisher does not
    // consume the combo points before calling this function !!
    public void relentlessStrikes()
    {
        if (m_relentlessStrikesEnabled)
        {
            // Get combo points and multiply up
            if (m_sim.m_rng.nextDouble() <= (m_relentlessStrikesChance * ((double) m_sim.m_target.m_comboPoints.m_comboPoints)))
            {
                this.addEnergy(25);
                m_relentlessStrikesStats.addEnergy(25);
            }
        }
    }

    public void tier8_2PieceBonus()
    {
        if (m_tier8bonus)
        {
            this.addEnergy(1);
            m_tier8_2PieceBonusStats.addEnergy(1);
        }
    }

    private void addEnergy(int p_delta)
    {
        int l_newEnergy = m_currentEnergy + p_delta;
        if (l_newEnergy > m_maxEnergy)
        {
            m_currentEnergy = m_maxEnergy;
            this.m_cappedEnergyStats.addEnergy(l_newEnergy - m_maxEnergy);
        }
        else
        {
            m_currentEnergy = l_newEnergy;
        }
    }

    public void useAbilityEnergy(int p_energyToUse)
    {
        m_currentEnergy -= (p_energyToUse - m_sim.m_player.m_overkill.energyReduction());
    }

    public void startEnergyRegen()
    {
        m_sim.m_eventList.add(new CEnergyRegenEvent(0));
    }

    public class CEnergyRegenEvent extends CEvent {

        CEnergyRegenEvent(double p_eventTime)
        {
            super(p_eventTime);
        }

        public ArrayList<CEvent>runEvent(CSim p_sim)
        {
            ArrayList<CEvent> l_newEventList = new ArrayList<CEvent>();
            l_newEventList.add(new CEnergyRegenEvent(p_sim.m_currentRunTime + m_vitality));
            p_sim.m_player.m_energyRegen.autoEnergyRegen();

            // Run AI
            p_sim.m_ai.processAI();

            return l_newEventList;
        }
    }
}
